model{
  for(i in 1:n.obs){ #ragged array/unbalanced panel
    d.credit[i] ~ dnorm(mu[i], tau.y[i])
      mu[i] <- a.unit[country[i]] + 
        b.l.credit[country[i]]*lag.credit[i] +
        b.l.top1*lag.ineq[i] + b.d.top1*d.ineq[i]+   
        b.cl*cum.left[i] + b.l.top1cl*lag.ineq[i]*cum.left[i] + b.d.top1cl*d.ineq[i]*cum.left[i]+
        b.l.pop*lag.pop[i] + b.d.pop*d.pop[i] +
        b.l.gdp*lag.gdp[i] + b.d.gdp*d.gdp[i]
    logsigma2.y[i] <- g.unit[country[i]] + g.year[years.d[i]] + g.euro*eurozone[i] #model for the variance
    tau.y[i] <- 1/exp(logsigma2.y[i])
    res[i]<-d.credit[i] - mu[i] #residuals
    y.pred[i] ~ dnorm(mu[i], tau.y[i]) #repredicting y  
  }
  
  for(j in 1:(n.countries)){#country RE in model for variance  
    g.unit[j] ~ dnorm(0, tau.vc)
    a.unit[j] ~ dnorm(mu.c, tau.country) #country RE
    b.l.credit[j] <- -1*temp[j]
    temp[j] ~ dbeta(1,1)
  }
  for(j in 1:n.years){#country RE in model for variance
    g.year[j] ~ dnorm(0, tau.vy) 
  }
  
# priors
  mu.c ~ dnorm(0,0.00001)
  sigma.country ~ dunif(0,50)
  tau.country <- pow(sigma.country,-2)
  sigma.vc ~ dunif(0,10) #variance for country RE in variance term
  sigma.vy ~ dunif(0,10) #variance for year RE in variance term
  tau.vc <- pow(sigma.vc, -2)
  tau.vy <- pow(sigma.vy, -2)
  b.l.top1~ dnorm(0,0.0001)
  b.d.top1~ dnorm(0,0.0001)
  b.cl ~ dnorm(0,0.0001)
  b.l.top1cl~ dnorm(0,0.0001)
  b.d.top1cl~ dnorm(0,0.0001)
  b.l.pop ~ dnorm(0,0.0001)
  b.d.pop ~ dnorm(0,0.0001)
  b.l.gdp ~ dnorm(0,0.0001)
  b.d.gdp ~ dnorm(0,0.0001)
  g.euro ~ dnorm (0,0.0001)
}

